import axios from "axios";

import { app } from "@/main";

const instance = axios.create({});

//请求拦截器
instance.interceptors.request.use(
    (config) => {
        // console.log("请求拦截器",config)
        //如果是不是用户接口请求，直接返回config
        // if (config.url.startsWith("/api/user") === false) {
        //     return config;
        // }
        const token = localStorage.getItem("token");
        if (token) {
            config.headers.Authorization = `Bearer ${token}`;
        }
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);
//响应拦截器
instance.interceptors.response.use(
    (res) => {
        // console.log(app.$showSessionExpiredDialog, "showSessionExpiredDialog");
        // console.log("响应拦截器", res);
        if (res.data.code === 408) {
            //登录超时
            app.$showSessionExpiredDialog();
            return Promise.reject(res.data);
        }
        return res.data;
    },
    (err) => {
        return Promise.reject(err);
    }
);

/**
 * 用户登录
 * @param {*} data = {username, password,type = "common" | "admin" | "traffic" } //用户登录信息
 * @returns
 */
function loginHttp(data) {
    return instance({
        url: "/api/user/login",
        method: "POST",
        data,
    });
}

/**
 * 用户注册
 * @param {*} data = {username, password,type = "common" | "admin" | "traffic" } //用户注册信息
 * @returns
 */
function registerHttp(data) {
    return instance({
        url: "/api/user/register",
        method: "POST",
        data,
    });
}

/**
 * 查询用户信息
 * @param {*} type = "common" | "admin" | "traffic" //用户类型
 */
function getUserInfoHttp(type) {
    return instance({
        url: "/api/user",
        method: "GET",
        params: {
            type,
        },
    });
}

/**
 * 退出登录
 * @param {*} data = {username,type = "common" | "admin" | "traffic" } //用户登录信息
 */
function logoutHttp({ username, type }) {
    return instance({
        url: "/api/user/logout",
        method: "POST",
        data: {
            username,
            type,
        },
    });
}

/**
 * 删除用户
 * @param {*} data = {username, type = "normal" | "admin" | "traffic" } //用户信息
 */
function deleteUserHttp(data) {
    return instance({
        url: "/api/user/delete",
        method: "POST",
        data,
    });
}
/**
 * 修改密码
 * @param {*} data = {username, type = "normal" | "admin" | "traffic" } //用户信息
 */
function changePasswordHttp({ username, password, type, newPassword }) {
    return instance({
        url: "/api/user/change",
        method: "POST",
        data: {
            username,
            password,
            type,
            newPassword,
        },
    });
}

/**
 * 修改用户信息
 * @param {*} data = {username,  oldType, newType, newPassword} //用户信息
 */
function modifyUserInfoHttp(data) {
    return instance({
        url: "/api/user/modify",
        method: "POST",
        data,
    });
}

/**
 * 查询公告信息
 *
 */
function getNoticeHttp() {
    return instance({
        url: "/api/notice",
        method: "GET",
    });
}

/**
 * 添加公告
 * @param {Object} data = {title, content,time} //公告内容
 */
function addNoticeHttp(data) {
    return instance({
        url: "/api/notice",
        method: "POST",
        data,
    });
}

/**
 * 查询报告信息
 *
 */
function getReportHttp() {
    return instance({
        url: "/api/roadcondition",
        method: "GET",
    });
}

/** 
*添加路况
*@param {Object} data = {name，type，address，signage，time，describe，status,position} //报告内容
*/
function addReportHttp(data) {
    return instance({
        url: "/api/roadcondition/add",
        method: "POST",
        data,
    });
}

/** 
*删除路况
*@param {Object} data = {id} //报告id
*/
function delReportHttp(data) {
    return instance({
        url: "/api/roadcondition/delete",
        method: "DELETE",
        params: data,
    });
}

/** 
*修改路况
*@param {Object} data = {id} //报告id
*/
function updateReportHttp(data) {
    return instance({
        url: "/api/roadcondition/update",
        method: "POST",
        data,
    });
}

//导出接口
export {
    loginHttp,
    registerHttp,
    getUserInfoHttp,
    logoutHttp,
    deleteUserHttp,
    getNoticeHttp,
    addNoticeHttp,
    
    getReportHttp,
    addReportHttp,
    delReportHttp,
    updateReportHttp,
    changePasswordHttp,
    modifyUserInfoHttp,
};
